#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
THN HTTP Stack Library
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 25-Apr-14 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.IO;
using Thn.Net.Http;
#endregion

namespace Demo
{
    /// <summary>
    /// Example of making a custom HTTP request handler to process
    /// incoming HTTP requests.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    internal
#endif
 class MyRequestHandler : IHttpRequestHandler
    {
        #region Process
        public void Process(IHttpRequestHandlingContext context)
        {
            //obtain the incoming HTTP request
            var request = context.Request;

            //create a new HTTP response
            var response = new HttpResponse();

            //mark this response's status as OK so that the client browser knows it went well
            //Since the default status is already OK, we don't have to assign it.
            //This is to let you know a different status can be assigned.
            //response.Status = HttpResponseStatus.OK;

            //choose an appropriate content type
            response.ContentType = "text/html";

            //obtain a writer to write to the response's body
            var writer = response.Body.Writer;

            //write the current time as a response message
            string txt = string.Format("Hello, the current time is {0}", DateTime.Now);
            writer.Write(txt, StringMode.Exact);//the string as is, without the length

            //send the response back to client
            //We have a couple of options to do so
            
            //Approach 1 - assign the response to context
            context.Response = response;

            //Approach 2 - send using the communication channel
            //This approach allows more freedom because we can choose when to send and how to send.
            //However, it will not pass the response along to other handlers that might be interested
            //in transforming outgoing messages.
            //context.Channel.Send(response);
        }

        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public MyRequestHandler()
            : base()
        { }
        #endregion
    }
}
